<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/28
 * Time: 10:29
 */

namespace app\admin\Logic;

use app\admin\Model\CommodityClassify;
use think\model\Merge;
use think\Db;
class CommodityClassLogic extends Merge
{
    public function goods_cat_list($cat_id = 0, $selected = 0, $re_type = true, $level = 0)
    {
        global $goods_category, $goods_category2;
        $goods_category=Db('commodity_classify')->where('delete_time',null)->order('parent_id asc,sort_order asc')->select();
        //将数据库中查出的列表以指定的 id 作为数组的键名
        $goods_category =convert_arr_key($goods_category,'id');
        foreach ($goods_category AS $key => $value)
        {
            if($value['level'] == 1)
                $this->get_cat_tree($value['id']);
        }
        /*
        foreach ($goods_category2 AS $key => $value)
        {
                $strpad_count = $value['level']*10;
                echo str_pad('',$strpad_count,"-",STR_PAD_LEFT);
                echo $value['name'];
                echo "<br/>";
        }*/
        return $goods_category2;
    }
  public function goods_cat_list2(){
      $goods_category=Db('commodity_classify')->where('delete_time',null)->order('parent_id asc,sort_order asc')->paginate();
      return $goods_category;
    }
    /**
     * 获取指定id下的 所有分类
     * @global type $goods_category 所有商品分类
     * @param type $id 当前显示的 菜单id
     * @return 返回数组 Description
     */
    public function get_cat_tree($id)
    {
        global $goods_category, $goods_category2;
        $goods_category2[$id] = $goods_category[$id];
        foreach ($goods_category AS $key => $value){
            if($value['parent_id'] == $id)
            {
                $this->get_cat_tree($value['id']);
                $goods_category2[$id]['have_son'] = 1; // 还有下级
            }
        }
    }


    /**
     * 移除指定$parent_id_path 分类以及下的所有分类
     * @global type $cat_list 所有商品分类
     * @param type $parent_id_path 指定的id
     * @return 返回数组 Description
     */
    public function remove_cat($cat_list,$parent_id_path)
    {
        foreach ($cat_list AS $key => $value){
            if(strstr($value['parent_id_path'],$parent_id_path))
            {
                unset($cat_list[$value['id']]);
            }
        }
        return $cat_list;
    }

    /**
     * 改变或者添加分类时 需要修改他下面的 parent_id_path  和 level
     * @global type $cat_list 所有商品分类
     * @param type $parent_id_path 指定的id
     * @return 返回数组 Description
     */
    public function refresh_cat($id)
    {
        $CommodityClassify = Db('CommodityClassify');
        $cat = $CommodityClassify->where("id",$id)->find(); // 找出他自己
        if ($cat['parent_id_path'] == ''){
            ($cat['parent_id'] == 0) && $CommodityClassify->execute('UPDATE commodity_classify set  parent_id_path = "0_$id", level = 1 where id = "$id"'); // 如果是一级分类
            $CommodityClassify->execute('update commodity_classify as a ,commodity_classify as b set  a.parent_id_path = concat_ws("_",b.parent_id_path,"$id"),a.level = (b.level+1) where a.parent_id=b.id and a.id = "$id"');
            $cat=$CommodityClassify->where("id",$id)->find();// 从新找出他自己
        }
        if($cat['parent_id'] == 0) //有可能是顶级分类 他没有老爸
        {
            $parent_cat['parent_id_path'] =   '0';
            $parent_cat['level'] = 0;
        }
        else{
            $parent_cat = $CommodityClassify->where("id",$cat['parent_id'])->find(); // 找出他老爸的parent_id_path
            dump($parent_cat);
        }
        $replace_level = $cat['level'] - ($parent_cat['level'] + 1); // 看看他 相比原来的等级 升级了多少  ($parent_cat['level'] + 1) 他老爸等级加一 就是他现在要改的等级
        $replace_str = $parent_cat['parent_id_path'].'_'.$id;
        $res=$CommodityClassify->execute("UPDATE `commodity_classify` SET parent_id_path = REPLACE(parent_id_path,'{$cat['parent_id_path']}','$replace_str'), level = (level - $replace_level) WHERE  parent_id_path LIKE '{$cat['parent_id_path']}%'");
        dump($res);
    }
    /**
     *  获取选中的下拉框
     * @param type $cat_id
     */
    function find_parent_cat($cat_id)
    {
        if ($cat_id == null)
            return array();
        $cat_list = Db('commodity_classify')->column('id,parent_id,level');
        $cat_level_arr[$cat_list[$cat_id]['level']] = $cat_id;
        // 找出他老爸
        $parent_id = $cat_list[$cat_id]['parent_id'];
        if ($parent_id > 0)
            $cat_level_arr[$cat_list[$parent_id]['level']] = $parent_id;
            //找出他爷爷
//          $grandpa_id = $cat_list[$parent_id]['parent_id'];
//        if($grandpa_id > 0)
//            $cat_level_arr[$cat_list[$grandpa_id]['level']] = $grandpa_id;
//            // 建议最多分 3级, 不要继续往下分太多级
//            // 找出他祖父
//            $grandfather_id = $cat_list[$grandpa_id]['parent_id'];
//        if($grandfather_id > 0)
//            $cat_level_arr[$cat_list[$grandfather_id]['level']] = $grandfather_id;
        return $cat_level_arr;
    }


    /**
     *  获取排好序的分类列表
     */
    function getSortCategory()
    {
        $categoryList =  M("GoodsCategory")->field('id,name,parent_id,level');
        $nameList = array();
        foreach($categoryList as $k => $v)
        {

            //$str_pad = str_pad('',($v[level] * 5),'-',STR_PAD_LEFT);
            $name = getFirstCharter($v['name']) .' '. $v['name']; // 前面加上拼音首字母
            //$name = getFirstCharter($v['name']) .' '. $v['name'].' '.$v['level']; // 前面加上拼音首字母
            /*
            // 找他老爸
            $parent_id = $v['parent_id'];
            if($parent_id)
                $name .= '--'.$categoryList[$parent_id]['name'];
            // 找他 爷爷
            $parent_id = $categoryList[$v['parent_id']]['parent_id'];
            if($parent_id)
                $name .= '--'.$categoryList[$parent_id]['name'];
            */
            $nameList[] = $v['name'] = $name;
            $categoryList[$k] = $v;
        }
        array_multisort($nameList,SORT_STRING,SORT_ASC,$categoryList);

        return $categoryList;
    }
}